package com.landian.commons.jpa.converter;

import org.springframework.util.CollectionUtils;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;

/**
 * @author caojl
 * @date: 2018-05-24
 * @time: 12:15
 */
public class PredicateConverter {

    /**
     * TODO 尝试重构为工具类 例子；Predicable[] arr = likeList.toArray(new Predicable[likeList.size()]);
     * 将Predicate不定参数转换为数组，null被忽略
     * @param predicates
     * @return
     */
    public static Predicate[] asPredicateArray(Predicate... predicates){
        List<Predicate> list = new ArrayList<>();
        if(null != predicates){
            for (Predicate predicate : predicates) {
                if(null != predicate){
                    list.add(predicate);
                }
            }
        }
        return asPredicateArray(list);
    }

    /**
     * asPredicateArray
     * @param list
     * @return
     */
    public static Predicate[] asPredicateArray(List<Predicate> list) {
        if(CollectionUtils.isEmpty(list)){
            return new Predicate[]{};
        }
        Predicate[] arr = new Predicate[list.size()];
        for(int i=0; i<list.size(); i++){
            arr[i] = list.get(i);
        }
        return arr;
    }
}
